// components/firing-temp/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    dataSource: {
      type: String,
      value: "{}",
      observer: 'setDataSource'
    },
    recordState: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
     temp: [
       {
         value: '',
         name: '底温',
         key: 'dw',
         area: {
           value: '',
           name: '区间',
           key: 'dw-a'
         }
     },
     {
      value: '',
      name: '面温',
      key: 'mw',
      area: {
        value: '',
        name: '区间',
        key: 'mw-a'
      }
  }
    ]
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
 
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    setDataSource() {
      const dataSource = JSON.parse(this.properties.dataSource || '{}') ;
      const obj = this.data.temp.map(it=>{
        return {
          ...it,
          value: dataSource[it.key],
          area: {
            ...it.area,
            value: dataSource[it.area.key]
          }
        }
      })
      this.setData({
        temp: obj
      })
    },
    onClose() {
      this.triggerEvent('close');
     },
     onSubmit({detail}) {
      const values = Object.keys(detail.value);
      const b = values.some(it => detail.value[it] === '');
      if(b) {
        wx.showToast({
          title: '存在应填项未填',
          icon: 'error'
        })
        return 
      }
      this.triggerEvent('onSubmit', detail);
     }
  }
})
